package com.simple.deploy.controller;

import com.simp.page.R;
import com.simp.utils.CommonUtils;
import com.simple.deploy.service.DeployService;
import com.simple.deploy.vo.DeployVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author pan.shun
 * @since 2022/3/17 14:59
 */
@Api(tags = "部署")
@CrossOrigin
@RestController
@RequestMapping("/deploy")
public class DeployController {

    @Autowired
    private DeployService deployService;

    @ApiOperation(value = "查询文件列表", notes = "获取前后文件列表")
    @GetMapping("/fileList")
    public R getFileList() {
        return deployService.getFileList();
    }

    @PostMapping("/uploadFile")
    public R uploadFile(@RequestParam("file") MultipartFile file, DeployVo deployVo) {
        return deployService.uploadFile(file, deployVo);
    }

    @ApiOperation(value = "查询文件内容", notes = "查询文件内容")
    @PostMapping("/queryFileContent")
    public R queryFileContent(@RequestBody DeployVo deployVo) {
        return deployService.queryFileContent(deployVo);
    }

    @ApiOperation(value = "执行命令", notes = "执行命令，传入一个cmd 指令")
    @PostMapping("/executeCommand")
    public R executeCommand(@RequestBody DeployVo deployVo) {
        return R.success(CommonUtils.executeCommand(deployVo.getCmd()));
    }

    @ApiOperation(value = "异步执行命令", notes = "异步执行命令，传入一个cmd 指令")
    @PostMapping("/executeCommandAsync")
    public R executeCommandAsync(@RequestBody DeployVo deployVo) {
        deployService.executeCommandAsync(deployVo.getCmd());
        return R.success(null, "执行完毕");
    }

    @ApiOperation(value = "文件删除")
    @PostMapping("/removeFile")
    public R removeFile(@RequestBody DeployVo deployVo) {
        return deployService.removeFile(deployVo);
    }


    @ApiOperation(value = "修改文件名")
    @PostMapping("/reNameFileName")
    public R reNameFileName(@RequestBody DeployVo deployVo) {
        return deployService.reNameFileName(deployVo);
    }

    @ApiOperation(value = "文件解压")
    @PostMapping("/unZip")
    public R unZipFile(@RequestBody DeployVo deployVo) {
        return deployService.unZipFile(deployVo);
    }
}
